package cn.psvmc.cxzapi.service.customer;

import cn.psvmc.cxzapi.mapper.customer.TCustomerMapper;
import cn.psvmc.cxzapi.mapper.order.TOrderMapper;
import cn.psvmc.cxzapi.model.customer.TCustomer;
import cn.psvmc.cxzapi.model.order.TOrder;
import cn.psvmc.cxzapi.response.Result;
import cn.psvmc.cxzapi.response.ResultGenerator;
import cn.psvmc.cxzapi.service.system.ServiceUtil;
import cn.psvmc.cxzapi.util.HttpUtil;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author ywx
 * Date 2024/8/17 10:40
 **/
@Service
@Slf4j
public class TCustomerAuthService {
    @Resource
    private TCustomerMapper customerMapper;
    @Resource
    private TOrderMapper orderMapper;
    @Resource
    private ServiceUtil serviceUtil;

    /**
     * @Description 生成授权书并上传至oss
     * @Date 2024/8/17 10:49
     * @Author YWX
     * @Param [authPeople]
     * @Return java.lang.String
     **/
    public String upPdfToOss (Object authPeople) {
        return serviceUtil.upPdfToOss(authPeople);
    }

    /**
     * @Description 获取信审报告
     * @Date 2024/8/17 10:49
     * @Author YWX
     * @Param [map]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    public Result getRiskReport(Map<String, Object> map) {
        map.put("oss_path", upPdfToOss(map.get("name")));
        String riskReport = HttpUtil.getRiskReport(map);
        return ResultGenerator.genSuccessResult("查询成功！", riskReport);
    }

    /**
     * @Description 生成信审报告
     * @Date 2024/9/6 8:29
     * @Author YWX
     * @Param [customerid]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    public Result generateReport(Integer orderId) {
        TOrder tOrder = orderMapper.selectById(orderId);
        if (tOrder == null) {
            return ResultGenerator.genFailResult("订单不存在！");
        }
        TCustomer tCustomer = customerMapper.selectById(tOrder.getCustomerid());
        if (tCustomer == null) {
            return ResultGenerator.genFailResult("客户不存在！");
        }
        String name = tCustomer.getName();
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("ident_number", tCustomer.getIdNumber());
        map.put("phone", tCustomer.getPhone());
        String ossPath = upPdfToOss(name);
        map.put("oss_path", ossPath);
        String riskReport = HttpUtil.getRiskReport(map);
        Map<String, Object> result = JSON.parseObject(riskReport, Map.class);
        result.put("oss_path", ossPath);

        //更新订单
        TOrder order = new TOrder();
        order.setOrderId(orderId);
        order.setControlReport(1);
        order.setReportContent(JSON.toJSONString(result));
        orderMapper.updateById(order);
        return ResultGenerator.genSuccessResult("生成成功！", result);
    }
}
